<template>
  <div>
    <el-button
      style="float: left; margin-left: 15px"
      type="danger"
      plain
      icon="el-icon-delete"
      size="small"
      @click="delAll"
      :disabled="this.multipleSelection.length == 0"
      >批量删除</el-button
    >
    <el-input
      style="width: 170px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入系统模块名称"
      v-model="module"
    />
    <el-input
      style="width: 170px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入用户名称"
      v-model="username"
    />
    <el-select
      v-model="businessType"
      style="width: 170px; float: left; margin-left: 15px"
      size="small"
      clearable
      placeholder="请选择操作类型"
    >
      <el-option
        v-for="item in options2"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-select
      v-model="reqMethod"
      style="width: 170px; float: left; margin-left: 15px"
      size="small"
      clearable
      placeholder="请选择请求方式"
    >
      <el-option
        v-for="item in options1"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-select
      v-model="status"
      style="width: 170px; float: left; margin-left: 15px"
      size="small"
      clearable
      placeholder="请选择操作状态"
    >
      <el-option
        v-for="item in options"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      style="float: left; margin-left: 15px"
      size="small"
      @click="initOperationLog"
      >搜索</el-button
    >
    <el-button
      style="float: left; margin-left: 15px; margin-bottom: 12px"
      icon="el-icon-refresh"
      size="small"
      @click="clearInput"
      >重置</el-button
    >
    <el-button
      type="warning"
      plain
      size="small"
      icon="el-icon-upload2"
      style="float: left; margin-left: 15px"
      @click="exportOperationLog"
      >导出</el-button
    >
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%; margin-top: 10px"
      :cell-style="rowClass"
      :header-cell-style="headClass"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"> </el-table-column>
      <el-table-column
        prop="username"
        label="用户名称"
        width="80"
      ></el-table-column>
      <el-table-column prop="ip" label="登录IP" width="100"></el-table-column>
      <el-table-column prop="location" label="登录地点" width="100">
      </el-table-column>
      <el-table-column prop="module" label="系统模块" width="140">
      </el-table-column>
      <el-table-column prop="businessType" label="操作类型" width="80">
      </el-table-column>
      <el-table-column prop="status" label="操作状态" width="80">
      </el-table-column>
      <el-table-column prop="reqMethod" label="请求方式" width="80">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="method"
        label="请求接口"
        width="100"
      >
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="reqUrl"
        label="请求URL"
        width="100"
      >
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="reqParam"
        label="请求参数"
        width="100"
      >
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="msg"
        label="返回消息"
        width="80"
      >
      </el-table-column>
      <el-table-column prop="time" label="操作日期" width="150">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 1.5%; float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 9]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 9,
      total: 0,
      username: "",
      status: "",
      options: ["成功", "失败"],
      options1: ["GET", "PUT", "POST", "DELETE"],
      options2: [
        "新增",
        "删除",
        "更新",
        "查看",
        "检索",
        "导出",
        "新增或更新",
        "强退用户",
      ],
      module: "",
      businessType: "",
      reqMethod: "",
      multipleSelection: [],
    };
  },
  created() {
    this.initOperationLog();
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initOperationLog();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initOperationLog();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    rowClass() {
      return "text-align: center;";
    },
    headClass() {
      return "text-align: center;background:#f8f8f9;";
    },
    clearInput() {
      location.reload();
    },
    exportOperationLog() {
      this.$downloadReq("/operationLog/export");
    },
    handleDelete(row) {
      this.$confirm(
        '此操作将删除用户名为"' + row.username + '"的数据, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        this.$delete("operationLog/?ids=" + row.id).then((res) => {
          if (res.code == 200) {
            this.initOperationLog();
          }
        });
      });
    },
    initOperationLog() {
      this.$get(
        "operationLog/" +
          this.currentPage +
          "/" +
          this.pageSize +
          "?username=" +
          this.username +
          "&status=" +
          this.status +
          "&reqMethod=" +
          this.reqMethod +
          "&module=" +
          this.module +
          "&businessType=" +
          this.businessType
      ).then((res) => {
        if (res.code == 200 && res.code) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
    delAll() {
      this.$confirm(
        '此操作将删除"' + this.multipleSelection.length + '"个数据, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        let ids = "?";
        this.multipleSelection.forEach((item) => {
          ids += "ids=" + item.id + "&";
        });
        this.$delete("operationLog/" + ids).then((res) => {
          if (res.code == 200) {
            this.initOperationLog();
          }
        });
      });
    },
  },
};
</script>

<style>
</style>